package com.djinfo.work.interceptors;

import com.djinfo.work.common.anno.Login;
import com.djinfo.work.common.enums.Action;
import com.djinfo.work.common.util.SpringUtil;
import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;


/**
 * 权限拦截器
 * 
 * @author
 * 
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {
	/**
	 * 在controller前拦截
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		/*String requestUri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = requestUri.substring(contextPath.length());


		HandlerMethod handlerMethod = (HandlerMethod) handler;
		Method method = handlerMethod.getMethod();
		Login login = method.getAnnotation(Login.class);
		if (login != null) {
			if (login.action() == Action.Skip) {
				return true;
			}
		}

		String userId = (String) request.getSession().getAttribute("userId");
		if(null == userId){
			// 跳转到登录页面
			response.sendRedirect("login.html");
			*//*request.getRequestDispatcher("login.html").forward(request, response);*//* *//*需要重定向*//*
			return false;
		}
		else{
			return true;
		}*/
		return true;
	}


	/**
	 * 在controller后拦截
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {
	/*	System.out.println("afterCompletion...");*/
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
		/*System.out.println("postHandle...");
		if(modelAndView != null){
			Map<String, String> map = new HashMap<String, String>();
			modelAndView.addAllObjects(map);
		}*/
	}

}
